/*
 * NewGroupInterface.java
 *
 * Created on May 11, 2006, 10:38 AM
 *
 * @author  Francois Daniels
 */

import java.awt.event.*;
import java.awt.*;
import za.gateway.preferences.Settings;
import za.gateway.io.*;
import za.gateway.backend.*;
import za.gateway.container.*;

public class NewGroupInterface extends javax.swing.JDialog 
        implements ActionListener {
    
    /** Creates new form NewGroupInterface */
    Groups GroupsContainer;
    Group [] GroupElements;
    
    
    public NewGroupInterface (java.awt.Frame parent, boolean modal) {
        super (parent, modal);
        initComponents ();
        updateComponents ();
    }
    
    public NewGroupInterface (javax.swing.JDialog parent, 
            boolean modal) {
        
        super (parent, modal);
        initComponents ();
        updateComponents ();
    }
    
    
    private void updateComponents () {
        cancelButton.addActionListener (this);
        if (Settings.isWindows) {
            try {
                javax.swing.UIManager.setLookAndFeel (
                "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                javax.swing.SwingUtilities.
                        updateComponentTreeUI (this);
                pack ();
            } catch (Exception e) { }
        }
    }
    
    public void setVisible (boolean b) {
        GroupsContainer = new Groups (Settings.userName, 
                Settings.password, Settings.IP, Settings.database);
        GroupElements = GroupsContainer.GetData ();
        
        availibleList.removeAll ();
        
        for (int i = 0; i < GroupElements.length; i++)
            availibleList.add (GroupElements [i].GetName ());
        
        centerOnParentAndShow ();
        super.setVisible (b);
    }
    
    public void actionPerformed (ActionEvent e) {
        if (e.getSource () == addButton) {
            //TOOD: Add code to add data to the data
            //Actualy the data and return will be tested
            //Use will be notified of any errors that occured during 
            //the processing of the event
            
            dispose ();
            GroupsContainer.Save ();
        } else
            if (e.getSource () == cancelButton) {
            dispose ();
            }
    }
    
    public void showWindow (String name, String password, 
            String description) {
        
        nameText.setText (name);
        passwordText.setText (password);
        descriptionText.setText (description);
        setTitle ("Edit Group Entry");
        setVisible (true);
    }
    
    private void centerOnParentAndShow () {
        pack ();
        Container parent = getParent ();
        if (parent == null) return;
        
        Dimension dparent = parent.getSize ();
        Dimension window = getSize ();
        int xCoord, yCoord;
        try {
            xCoord = parent.getLocationOnScreen ().x + 
                    (dparent.width/2 - window.width/2);
            yCoord = parent.getLocationOnScreen ().y + 
                    (dparent.height/2 - window.height/2);
        } catch (IllegalComponentStateException e) {return;}
        
        //Ensure that no part of the window will be off-screen
        Dimension screen = Toolkit.getDefaultToolkit ().
                getScreenSize ();
        
        int xOffScreenExcess = xCoord + window.width - screen.width;
        if ( xOffScreenExcess > 0 ) {
            xCoord = xCoord - xOffScreenExcess;
        }
        if (xCoord < 0 ) {
            xCoord = 0;
        }
        int yOffScreenExcess = yCoord + window.height - screen.height;
        if ( yOffScreenExcess > 0 ) {
            yCoord = yCoord - yOffScreenExcess;
        }
        if (yCoord < 0) {
            yCoord = 0;
        }
        
        setLocation ( xCoord, yCoord );
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Code generated by Netbeans for the GUI Components  ">
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        javax.swing.JLabel descLabel;
        javax.swing.JLabel nameLabel;
        javax.swing.JLabel passwordLabel;

        nameLabel = new javax.swing.JLabel();
        nameText = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        passwordText = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        descLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionText = new javax.swing.JTextArea();
        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        remRestrictionButton = new javax.swing.JButton();
        addRestrictionButton = new javax.swing.JButton();
        currentList = new java.awt.List();
        availibleList = new java.awt.List();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Group Entry");
        setResizable(false);
        nameLabel.setText("Name:");

        passwordLabel.setText("Password:");

        descLabel.setText("Description:");

        descriptionText.setColumns(20);
        descriptionText.setRows(5);
        jScrollPane1.setViewportView(descriptionText);

        addButton.setText("Add");
        addButton.setMaximumSize(new java.awt.Dimension(52, 23));
        addButton.setMinimumSize(new java.awt.Dimension(52, 23));
        addButton.setPreferredSize(new java.awt.Dimension(52, 23));
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Inhereted Restrictions"));
        jLabel1.setText("Availible Restrictions");

        jLabel2.setText("Current Restrictions");

        remRestrictionButton.setText("<- Remove");
        remRestrictionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remRestrictionButtonActionPerformed(evt);
            }
        });

        addRestrictionButton.setText("Add ->");
        addRestrictionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addRestrictionButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(availibleList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(remRestrictionButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                    .add(addRestrictionButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel2)
                        .add(38, 38, 38))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(currentList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(currentList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, availibleList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(addRestrictionButton)
                        .add(20, 20, 20)
                        .add(remRestrictionButton)))
                .add(104, 104, 104))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(nameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(nameText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 165, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(43, 43, 43)
                        .add(passwordLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(passwordText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
                    .add(descLabel)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(addButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 71, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 305, Short.MAX_VALUE)
                        .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(passwordText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(passwordLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 120, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(descLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cancelButton))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        String Name = nameText.getText ();
        String Pass = passwordText.getText ();
        String Desc = descriptionText.getText ();
        int [] indexes = currentList.getSelectedIndexes ();
        String [] Parents = new String [indexes.length];
        
        for (int i = 0; i < indexes.length; i++)
            Parents [i] = currentList.getItem (indexes [i]);
        
        GroupsContainer.Add (Name,Pass,Parents,Desc);
        GroupsContainer.Save ();
        dispose ();
    }//GEN-LAST:event_addButtonActionPerformed
    
    private void remRestrictionButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remRestrictionButtonActionPerformed
        int [] indexes = currentList.getSelectedIndexes ();
        String [] names = new String [indexes.length];
        
        for (int i = 0; i < indexes.length; i++) {
            names [i] = currentList.getItem (indexes [i]);
            currentList.remove (names [i]);
            availibleList.add (names [i]);
        }
    }//GEN-LAST:event_remRestrictionButtonActionPerformed
    
    private void addRestrictionButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addRestrictionButtonActionPerformed
        int [] indexes = availibleList.getSelectedIndexes ();
        String [] names = new String [indexes.length];
        
        for (int i = 0; i < indexes.length; i++) {
            names [i] = availibleList.getItem (indexes [i]);
            availibleList.remove (names [i]);
            currentList.add (names [i]);
        }
        
    }//GEN-LAST:event_addRestrictionButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main (String args[]) {
        java.awt.EventQueue.invokeLater (new Runnable () {
            public void run () {
                new NewGroupInterface (new javax.swing.JFrame (), 
                        true).setVisible (true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton addRestrictionButton;
    private java.awt.List availibleList;
    private javax.swing.JButton cancelButton;
    private java.awt.List currentList;
    private javax.swing.JTextArea descriptionText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private java.awt.List list2;
    private javax.swing.JTextField nameText;
    private javax.swing.JTextField passwordText;
    private javax.swing.JButton remRestrictionButton;
    // End of variables declaration//GEN-END:variables
    
}
